package JavaSE.d3;

import java.util.Random;
import java.util.Scanner;
/*
中学生考试系统
设计10道四则运算的题目，
两个操作数均在20以内，生成的题干可能是加、减、乘、除中的任何一种运算，逐个生成题干，从键盘输入答案，
如果正确，则输出”答案正确，加10分“字样，如果答案错误，则输出”很遗憾，加油“字样，最后统计测验成绩。
 */
public class Test29 {
    static int soc=0;

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
//            MathOPeration.division();除法模块测试
            MathOPeration.operation();
        }
        System.out.println("你获得了"+soc+"分");
    }
    class MathOPeration{

        static void operation(){
            Random random = new Random();
            int jude=random.nextInt(1,20);
            if(0<=jude&&jude<5){
                MathOPeration.addtion();
            }else if(5<=jude&&jude<10){
                MathOPeration.subtraction();
            }else if(10<=jude&&jude<15){
                MathOPeration.multiplication();
            }else if(15<=jude&&jude<21){
                MathOPeration.division();
            }
        }

        static void addtion(){
            Random random = new Random();
            Scanner scanner = new Scanner(System.in);
            int x=random.nextInt(0,20);
            int y=random.nextInt(0,20);

            double asw=x+y;
            System.out.println(x+"+"+y+"=?");
            System.out.println(asw);
            double que=scanner.nextDouble();
            if(asw==que){
                System.out.println("你答对了");
                soc+=10;
            }else{
                System.out.println("你答错了");
            }


        }

        static void subtraction(){
            Random random = new Random();
            Scanner scanner = new Scanner(System.in);
            int x=random.nextInt(0,20);
            int y=random.nextInt(0,20);

            if(x>y){
                double asw=x-y;
                System.out.println(x+"-"+y+"=?");
                System.out.println(asw);
                double que=scanner.nextDouble();
                if(asw==que){
                    System.out.println("你答对了");
                    soc+=10;
                }else{
                    System.out.println("你答错了");
                }
            }else {
                double asw=y-x;
                System.out.println(y+"-"+x+"=?");
                System.out.println(asw);
                double que=scanner.nextDouble();
                if(asw==que){
                    System.out.println("你答对了");
                    soc+=10;
                }else{
                    System.out.println("你答错了");
                }
            }

        }

        static void multiplication(){
            Random random = new Random();
            Scanner scanner = new Scanner(System.in);
            int x=random.nextInt(0,20);
            int y=random.nextInt(0,20);

            double asw=x*y;
            System.out.println(x+"*"+y+"=?");
            System.out.println(asw);
            double que=scanner.nextDouble();
            if(asw==que){
                System.out.println("你答对了");
                soc+=10;
            }else{
                System.out.println("你答错了");
            }

        }
        //最大公约数
        public static int gcd(int a,int b){
            if(b==0){
                return a;
            }else {
                return (a%b==0)?b:gcd(b,a%b);
            }
        }
        //最小公倍数
        public static int lcm(int x,int y){
            int gcd=gcd(x,y);
            return (x*y/gcd);
        }
        static void division(){
            Random random = new Random();
            Scanner scanner = new Scanner(System.in);
            int x=random.nextInt(0,20);
            int y=random.nextInt(0,20);

            if(x!=0&&y!=0){
                if(x/gcd(x,y)%y/gcd(x,y)==0){//最简分数形式以字符串输出，能化整数化整数以数字输出，
                    int asw=(x/gcd(x,y))/(y/gcd(x,y));
                    if((x/gcd(x,y))%(y/gcd(x,y))==0){
                        System.out.println(x+"/"+y+"=?");
                        System.out.println(asw);

                        int que=scanner.nextInt();
                        if(asw==que){
                            System.out.println("你答对了");
                            soc+=10;
                        }else{
                            System.out.println("你答错了");
                        }

                    }else {
                        String as=x+"/"+y;
                        System.out.println(x+"/"+y+"=?");
                        System.out.println(as);

                        String que=scanner.nextLine();
                        if(as.equals(que)){
                            System.out.println("你答对了");
                            soc+=10;
                        }else{
                            System.out.println("你答错了");
                        }
                    }

                }else {
                    String asw=x/gcd(x,y)+"/"+y/gcd(x,y);
                    System.out.println(x+"/"+y+"=?");
                    System.out.println(asw);

                    String que=scanner.nextLine();
                    if(asw.equals(que)){
                        System.out.println("你答对了");
                        soc+=10;
                    }else{
                        System.out.println("你答错了");
                    }
                }


            }else {
                division();
            }
        }

    }
}






















